/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.slogger.core;

import com.hyts.slogger.base.SAppender;
import com.hyts.slogger.base.SFilter;
import com.hyts.slogger.base.SFormatter;
import com.hyts.slogger.config.LogLevel;
import com.hyts.slogger.filter.LogLevelFilter;
import sun.reflect.Reflection;

import java.io.IOException;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.slogger.core
 * @author:LiBo/Alex
 * @create-date:2019-08-06 13:57
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description: 同步日志操作实现类
 */

public class SyncLoggerImpl extends SLoggerImpl {


    /**
     * 同步日志实现构造器
     * @param async
     * @param sFormatter
     * @param sAppender
     * @param sFilter
     */
    SyncLoggerImpl(boolean async, SFormatter sFormatter, SAppender sAppender, SFilter sFilter) {
        super(async, sFormatter, sAppender,sFilter);
    }

    /**
     * 同步日志实现构造器
     * @param async
     * @param sFormatter
     * @param sAppender
     */
    SyncLoggerImpl(boolean async, SFormatter sFormatter, SAppender sAppender) {
        super(async, sFormatter, sAppender,new LogLevelFilter());
    }

    /**
     * 同步日志实现构造器
     * @param sFormatter
     * @param sAppender
     */
    public SyncLoggerImpl(SFormatter sFormatter, SAppender sAppender){
        this(false,sFormatter,sAppender);
    }

    /**
     * trace - 链路操作
     * @param message
     */
    @Override
    public void trace(String message) {
        //过滤器操作级别控制
        if(sFilter.filter(LogLevel.TRACE)) {
            //操作log header日志头的功能
            String logHeader = sFormatter.logHead(null, null);
            //操作log tail日志头的功能
            String logTailer = sFormatter.logTail(null, null);
            try {
                ((SAppender) sAppender.append(logHeader.toString()).append(message.toString()).append(logTailer.toString())).
                        flush(false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * trace - 链路操作
     * @param message
     * @param args
     */
    @Override
    public void trace(String message, Object... args) {
        //过滤器操作级别控制
        if(sFilter.filter(LogLevel.TRACE)) {
            //操作log header日志头的功能
            String logHeader = sFormatter.logHead(null, null);
            //操作log tail日志头的功能
            String logTailer = sFormatter.logTail(null, null);
            //日志消息体格式化操作服务
            message = sFormatter.logFormat(message, args);
            try {
                ((SAppender) sAppender.append(logHeader).append(message).append(logTailer)).
                        flush(false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * debug级别操作服务控制处理消息
     * @param message
     */
    @Override
    public void debug(String message) {
        //过滤器操作级别控制
        if(sFilter.filter(LogLevel.DEBUG)) {
            //操作log header日志头的功能
            String logHeader = sFormatter.logHead(null, null);
            //操作log tail日志头的功能
            String logTailer = sFormatter.logTail(null, null);
            //日志消息体格式化操作服务
            message = sFormatter.logFormat(message);
            try {
                ((SAppender) sAppender.append(logHeader).append(message).append(logTailer)).
                        flush(false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * debug级别操作服务控制处理消息
     * @param message
     * @param args
     */
    @Override
    public void debug(String message, Object... args) {
        //过滤器操作级别控制
        if(sFilter.filter(LogLevel.DEBUG)) {
            //操作log header日志头的功能
            String logHeader = sFormatter.logHead(null, null);
            //操作log tail日志头的功能
            String logTailer = sFormatter.logTail(null, null);
            //日志消息体格式化操作服务
            message = sFormatter.logFormat(message,args);
            try {
                ((SAppender) sAppender.append(logHeader).append(message).append(logTailer)).
                        flush(false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void info(String message) {
        //过滤器操作级别控制
        if(sFilter.filter(LogLevel.INFO)) {
            //操作log header日志头的功能
            String logHeader = sFormatter.logHead(null, null);
            //操作log tail日志头的功能
            String logTailer = sFormatter.logTail(null, null);
            //日志消息体格式化操作服务
            message = sFormatter.logFormat(message);
            try {
                ((SAppender) sAppender.append(logHeader).append(message).append(logTailer)).
                        flush(false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void info(String message, Object... args) {
        //过滤器操作级别控制
        if(sFilter.filter(LogLevel.INFO)) {
            //操作log header日志头的功能
            String logHeader = sFormatter.logHead(null, null);
            //操作log tail日志头的功能
            String logTailer = sFormatter.logTail(null, null);
            //日志消息体格式化操作服务
            message = sFormatter.logFormat(message,args);
            try {
                ((SAppender) sAppender.append(logHeader).append(message).append(logTailer)).
                        flush(false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void warn(String message) {

    }

    @Override
    public void warn(String message, Object... args) {

    }

    @Override
    public void warn(String message, Throwable e) {

    }

    @Override
    public void error(String message) {

    }

    @Override
    public void error(String message, Object... args) {

    }

    @Override
    public void error(String message, Throwable e) {

    }
}
